import { Component } from '@angular/core';
// import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { InAppBrowser } from '@ionic-native/in-app-browser';
// import { environment } from '../../environments/env.dev';
// import { CommonHttpService } from '../../service/http-service/common/common-http-service';
import {ApplicantService} from "../../service/http-service/applicant.service";
import {IonicPage,NavController} from 'ionic-angular';
// import {platformBrowser} from "@angular/platform-browser";


/**
 * Generated class for the MystatePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage(
  // {
  //   // priority: 'high'
  //   // name: 'PageproxyPage',
  //   // segment: 'pageproxy',
  //   // defaultHistory: ['HomePage'],
  // }

)
@Component({
  selector: 'page-pageproxy',
  templateUrl: 'pageproxy.html',
})
export class PageproxyPage {
 // vid1: string;
 // vid2: string;
 usrInfo1: any;
 code:any;
 timeStamp: any;
 // rd: string;
 resultIndex: number;

//  usrInfo = [{id:'1', name: "lide"},{id:'2', name: "lide1"}];
 usrInfo = [];

 public homeUrl: string;




  constructor(public iab:InAppBrowser, public navCtrl: NavController, public httpClient: ApplicantService) {



  //  const browser =this.iab.create('http://localhost:8100/#/listvido/2', '_self', {location: 'yes'});
  //  browser.close();
  // console.log(this.navCtrl.last());
  }


  ionViewWillEnter(){
    console.log("2.0 ionViewWillEnter 顾名思义，当将要进入页面时触发");
    // window.location.reload();

  this.code = this.GetQueryString('code')
  // console.log(this.GetQueryString('code'));
  localStorage.setItem('code',this.code);



  // console.log(localStorage.getItem('code'));





 console.log(this.homeUrl);
 this.httpClient.get2('wxauth/wxLogin',this.code).subscribe(resp=>{
   console.log("get the user data")
   if (resp.data!==null){

     console.log(resp.data);
     try {
       this.usrInfo=resp.data;
       var str = JSON.stringify(this.usrInfo);

       localStorage.setItem('options',str);
       this.usrInfo1 = JSON.parse(localStorage.getItem('options'));
       if(this.usrInfo1!=null&&this.usrInfo1!=undefined) {
         localStorage.setItem('token', this.usrInfo1.token);
         localStorage.setItem('openid', this.usrInfo1.openid);
         localStorage.setItem('phone', this.usrInfo1.phone);
         localStorage.setItem('id', this.usrInfo1.id);
         if(this.usrInfo1.token!=null&&this.usrInfo1.token!=undefined){
           this.timeStamp = new Date().getTime();
           window.localStorage.setItem('timeStamp2', this.timeStamp);
         }else{localStorage.setItem('timeStamp2', null); }
       }
     }catch (e){
       alert("error:"+e);
       console.error("==>error:"+e);
     }finally {
       window.history.back();
       window.history.back();
     }


   }else {

     console.log("resp.data==null");

     window.history.back();
     window.history.back();
     window.history.back();
     // this.navCtrl.remove(this.navCtrl.last().index, 1);
     // this.navCtrl.pop();

   }
 },error2 => {

   console.log("errorlllllllllllllllllllll")
   console.log(error2);
   window.history.back();
   window.history.back();
   window.history.back();
   // window.history.back();
   // window.history.back();
   // this.navCtrl.remove(this.navCtrl.last().index, 1);
   // this.navCtrl.pop();


 });





}



  GetQueryString(name)
  {
       var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
       var r= window.location.search.substr(1).match(reg);
      // this.vid1 = '?code=071zrjxT173Yg81otkyT1f6oxT1zrjxy&state=1';
      // var r = this.r.substr(1).match(reg);
       if(r!=null)return  decodeURIComponent(r[2]); return null;
  }



  }



